package com.deyuanyun.pic.pipe.service.impl;

import com.deyuanyun.pic.common.dto.BasicDataByFind;
import com.deyuanyun.pic.common.dto.DTOResult;
import com.deyuanyun.pic.common.exception.ExceptionCode;
import com.deyuanyun.pic.common.exception.PicException;
import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.pipe.mapper.dao.PiggingStructureDao;
import com.deyuanyun.pic.pipe.service.PiggingStructureService;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.*;

/**
 * 
 * @ClassName ClosureServiceImpl
 * @Description 管道基础数据 ->收发球筒service实现
 * @author 荣华兴
 * @date 2015年8月31日
 */
@Service
public class PiggingStructureServiceImpl implements PiggingStructureService {

	@Resource
	PiggingStructureDao piggingStructureDao;

	/**
	 * @Description: 根据条件查询
	 * @Author: 荣华兴
	 * @Create Date: 2015年8月31日
	 * @参数：@param bData，对象中包含：管道id、管段id、起始里程、终点里程
	 * @Return:DTOResult
	 */
	@Override
	public DTOResult findByCondition(BasicDataByFind bData) {

		Page<List> list = null;

		try {
			list = piggingStructureDao.findByCondition(bData);

		} catch (SQLException ex) {

			ex.printStackTrace();
		}

		return new DTOResult(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getLabel(), list);

	}

	/**
	 * @Description: 根据传入Id，查询对应Id的详细数据
	 * @Author: 荣华兴
	 * @Create Date: 2015年8月31日
	 * @参数：@param id
	 * @参数：@return
	 * @Return:DTOResult
	 */
	@Override
	public DTOResult findById(String id) {
		List list = null;

		try {
			list = piggingStructureDao.findById(id);

		} catch (SQLException ex) {

			ex.printStackTrace();

		}
		return new DTOResult(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getLabel(), list);

	}

	/**
	 * @Description: 根据页面传入的值，查询导出值
	 * @Author: 荣华兴
	 * @Create Date: 2015年8月31日
	 * @参数：@param bData，对象中包含：管道id、管段id、起始里程、终点里程
	 * @Return:List<List<String[]>> 收发球筒导出列表
	 */
	@Override
	public List<List<String[]>> excelExport(BasicDataByFind bData) {
		List<List<String[]>> result = new ArrayList<List<String[]>>();
		ArrayList<LinkedHashMap<String, Object>> list = new ArrayList<LinkedHashMap<String, Object>>();

		// 将数据取出放入list中
		list = piggingStructureDao.excelExport(bData);
		if (list.size() == 0) {
			list = null;
		}
		result.add(JXL.toList(list, "yyyy-MM-DD"));
		return result;
	}
	
	/**
	 * 获取GISid
	 */
	@Override
	public DTOResult getGis(BasicDataByFind base) {
		List list = null;

		try {
			list = piggingStructureDao.getGis(base);

		} catch (PicException ex) {

			ex.printStackTrace();
		}

		return new DTOResult(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getLabel(), list);

	}

	@Override
	public Map<String,Object> findStructureByLineId(String lineId,String beginMileage,String endMileage) {
		Map<String,Object> result=new HashMap<String,Object>();

		Map<String,Object> param=new HashMap<String,Object>();
		param.put("lineId",lineId);
		param.put("type","begin");
		param.put("beginMileage","beginMileage");
		param.put("endMileage","endMileage");
		result.put("begin", piggingStructureDao.findStructureByLineIdOrMileage(param));
		param.put("type","end");
		result.put("end", piggingStructureDao.findStructureByLineIdOrMileage(param));


		return result;
	}
}
